package com.cashmate.struts;

import com.cashmate.hibernate.dao.LoanverificationDAO;
import com.cashmate.hibernate.pojo.Loanverification;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

public class Request_LoadRequest extends ActionSupport {

    private LoanverificationDAO loanver = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    private Integer records = 0;
    private Integer id;
    private String sidx;
    // Search Field
    private String searchField;
    // The Search String
    private String searchString;
    // he Search Operation ['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc']
    private String searchOper;
    // Your Total Pages
    private Integer total = 0;
    //Your result List
    private List<Loanverification> gridModel;
    //get how many rows we want to have into the grid - rowNum attribute in the grid
    private Integer rows = 0;
    //Get the requested page. By default grid sets this to 1.
    private Integer page = 0;
    // sorting order - asc or desc
    private String sord;
    private boolean loadonce = false;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public boolean getLoadonce() {
        return loadonce;
    }
    
    public void setLoadonce(boolean loadonce) {
        this.loadonce = loadonce;
    }

    public String getSearchField() {
        return searchField;
    }

    public void setSearchField(String searchField) {
        this.searchField = searchField;
    }

    public String getSearchOper() {
        return searchOper;
    }

    public void setSearchOper(String searchOper) {
        this.searchOper = searchOper;
    }

    public String getSearchString() {
        return searchString;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }

    public List<Loanverification> getGridModel() {
        return gridModel;
    }

    public void setGridModel(List<Loanverification> gridModel) {
        this.gridModel = gridModel;
    }

    public Integer getPage() {
        return page;
    }

    public String getSidx() {
        return sidx;
    }

    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    public String getSord() {
        return sord;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRecords() {
        return records;
    }

    public void setRecords(Integer records) {
        this.records = records;
        if (this.records > 0 && this.rows > 0) {
            this.total = (int) Math.ceil((double) this.records / (double) this.rows);
        } else {
            this.total = 0;
        }
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }
    // All Record

    @Actions({
        @Action(value = "/step", results = {
            @Result(name = "success", type = "json")
        })
    })
    @Override
    public String execute() {
        try {
            
            /**
             * eto ung subgrid ng loanverification sa isang request
             */
            
            int to = 0;
            int from = 0;        
            
            System.out.println(id);
            
                DetachedCriteria criteria = DetachedCriteria.forClass(Loanverification.class);
                criteria.createCriteria("request", "req").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
                criteria.add(Restrictions.eq("req.requestid", id));

                records = loanver.countByCriteria(criteria);
                criteria.setProjection(null);
                criteria.setResultTransformer(Criteria.ROOT_ENTITY);
                    
                to = records;
                    
                gridModel = loanver.findByCriteria(criteria, from, to);
            


            // Calculate total Pages
            total = (int) Math.ceil((double) records / (double) rows);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public String getJSON() {
        return execute();
    }
}
